package org.jash.mvp2206.user;

import org.jash.mvp2206.mvp.BasePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;

public class RegisterPresenter extends BasePresenter<Construct.IRegisterView> implements Construct.IRegisterPresenter {
    private Construct.IRegisterModel model = new RegisterModel();
    @Override
    public void getVerifyCode(String phone) {
        disposable.add(model.getVerifyCode(phone).observeOn(AndroidSchedulers.mainThread())
                .subscribe(r -> {
                    if (isAttach()) {
                        if (r.getCode() == 0) {
                            getWhoView().codeSuccess(r.getData());
                        } else {
                            throw new RuntimeException(r.getMsg());
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }

    @Override
    public void checkCode(String phone, String code) {
        disposable.add(model.checkCode(phone, code).observeOn(AndroidSchedulers.mainThread())
                .subscribe(r -> {
                    if (isAttach()) {
                        if (r.getCode() == 0) {
                            getWhoView().checkedSuccess("验证通过");
                        } else {
                            throw new RuntimeException(r.getMsg());
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }

    @Override
    public void register(String username, String password, String phone) {
        disposable.add(model.register(username, password, phone).observeOn(AndroidSchedulers.mainThread())
                .subscribe(r -> {
                    if (isAttach()) {
                        if (r.getCode() == 0) {
                            getWhoView().registerSuccess("注册成功");
                        } else {
                            throw new RuntimeException(r.getMsg());
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }
}
